package com.fable.base;

import com.fable.base.ComponentException.ErrCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * Assertion utility class that assists in validating arguments. Useful for
 * identifying programmer errors early and clearly at runtime.
 */
public final class Assert {

    private Assert() {
    }

    /**
     * Assert that an object is <code>null</code> .
     *
     * @param object the object to check
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the object is not <code>null</code>
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert that an object is <code>null</code> .
     *
     * @param object the object to check
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the object is not <code>null</code>
     */
    public static void isNull(Object object, ErrCode errCode, String message) {
        if (object != null) {
            throwException(errCode, message);
        }
    }

    /**
     * Assert that an object is not <code>null</code> .
     * <pre class="code">Assert.notNull(clazz, "The class must not be
     * null");</pre>
     *
     * @param object the object to check
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the object is <code>null</code>
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert that an object is not <code>null</code> .
     * <pre class="code">Assert.notNull(clazz, "The class must not be
     * null");</pre>
     *
     * @param object the object to check
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the object is <code>null</code>
     */
    public static void notNull(Object object, ErrCode errCode, String message) {
        if (object == null) {
            throwException(errCode, message);
        }
    }

    /**
     * Assert that the given String is blank; that is, it must be
     * <code>null</code> or must not contain any non-whitespace character.
     * <pre class="code">Assert.isBlank(name, "'name' must be empty");</pre>
     *
     * @param text the String to check
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is not blank
     */
    public static void isBlank(String text, String message) {
        if (StringUtils.isNotBlank(text)) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert that the given String is blank; that is, it must be
     * <code>null</code> or must not contain any non-whitespace character.
     * <pre class="code">Assert.isBlank(name, "'name' must be empty");</pre>
     *
     * @param text the String to check
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is not blank
     */
    public static void isBlank(String text, ErrCode errCode, String message) {
        if (StringUtils.isNotBlank(text)) {
            throwException(errCode, message);
        }
    }

    /**
     * Assert that the given String is not blank; that is, it must not be
     * <code>null</code> and must contain at least one non-whitespace character.
     * <pre class="code">Assert.notBlank(name, "'name' must not be
     * empty");</pre>
     *
     * @param text the String to check
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is blank
     */
    public static void notBlank(String text, String message) {
        if (StringUtils.isBlank(text)) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert that the given String is not blank; that is, it must not be
     * <code>null</code> and must contain at least one non-whitespace character.
     * <pre class="code">Assert.notBlank(name, "'name' must not be
     * empty");</pre>
     *
     * @param text the String to check
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is blank
     */
    public static void notBlank(String text, ErrCode errCode, String message) {
        if (StringUtils.isBlank(text)) {
            throwException(errCode, message);
        }
    }

    /**
     * Assert a boolean expression, throwing
     * <code>IllegalArgumentException</code> if the test result is
     * <code>false</code>.
     * <pre class="code">Assert.isTrue(i &gt; 0, "The value must be greater than
     * zero");</pre>
     *
     * @param expression a boolean expression
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if expression is <code>false</code>
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert a boolean expression, throwing
     * <code>IllegalArgumentException</code> if the test result is
     * <code>false</code>.
     * <pre class="code">Assert.isTrue(i &gt; 0, "The value must be greater than
     * zero");</pre>
     *
     * @param expression a boolean expression
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if expression is <code>false</code>
     */
    public static void isTrue(boolean expression, ErrCode errCode, String message) {
        if (!expression) {
            throwException(errCode, message);
        }
    }

    /**
     * Assert that the given collection is empty; that is, it must not be
     * <code>null</code> and must contain at least one element.
     * <pre class="code">Assert.isEmpty(list, "'list' must be empty");</pre>
     *
     * @param collection the collection to check
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is not empty
     */
    public static void isEmpty(Collection collection, String message) {
        if (collection != null && !collection.isEmpty()) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert that the given collection is empty; that is, it must not be
     * <code>null</code> and must contain at least one element.
     * <pre class="code">Assert.isEmpty(list, "'list' must be empty");</pre>
     *
     * @param collection the collection to check
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is not empty
     */
    public static void isEmpty(Collection collection, ErrCode errCode, String message) {
        if (collection != null && !collection.isEmpty()) {
            throwException(errCode, message);
        }
    }

    /**
     * Assert that the given collection is not empty; that is, it must be
     * <code>null</code> or must contain no element.
     * <pre class="code">Assert.notEmpty(list, "'list' must not be
     * empty");</pre>
     *
     * @param collection the collection to check
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is not empty
     */
    public static void notEmpty(Collection collection, String message) {
        if (collection == null || collection.isEmpty()) {
            throwException(ErrCode.参数非法, message);
        }
    }

    /**
     * Assert that the given collection is not empty; that is, it must be
     * <code>null</code> or must contain no element.
     * <pre class="code">Assert.notEmpty(list, "'list' must not be
     * empty");</pre>
     *
     * @param collection the collection to check
     * @param errCode the error code to use if the assertion fails
     * @param message the exception message to use if the assertion fails
     * @throws ComponentException if the given string is not empty
     */
    public static void notEmpty(Collection collection, ErrCode errCode, String message) {
        if (collection == null || collection.isEmpty()) {
            throwException(errCode, message);
        }
    }

    private static void throwException(ErrCode errCode, String message) {
        throw new ComponentException(errCode, message);
    }

}
